1 #pragma warning disable 1587
2 ///
\file
3 ///
<summary>Part of the [Optional GUI](@ref optionalGui).</summary>
4 #pragma warning restore
1587
5
6
7 using
ExitGames.Client.Photon;
8 using
UnityEngine;
9
10
11 ///
<summary>
12 ///
Basic GUI to show traffic and health statistics of the connection to Photon,
13 ///
toggled by shift+tab.
14 ///
</summary>
15 ///
<remarks>
16 ///
The shown health values can help identify problems with connection losses or performance.
17 ///
Example:
18 ///
If the time delta between two consecutive SendOutgoingCommands calls is a second or more,
19 ///
chances rise for a disconnect being caused by this (because acknowledgements to the server
20 ///
need to be sent in due time).
21 ///
</remarks>
22 ///
\ingroup optionalGui
23 public
class PhotonStatsGui : MonoBehaviour
24 {

25     ///
<summary>Shows or hides GUI (does not affect if stats are collected).</summary>
26     
public bool statsWindowOn = true;
27
28     ///
<summary>Option to turn collecting stats on or off (used in Update()).</summary>
29     
public bool statsOn = true;
30
31     ///
<summary>Shows additional "health" values of connection.</summary>
32     
public bool healthStatsVisible;
33
34     ///
<summary>Shows additional "lower level" traffic stats.</summary>
35     
public bool trafficStatsOn;
36
37     ///
<summary>Show buttons to control stats and reset them.</summary>
38     
public bool buttonsOn;
39
40     ///
<summary>Positioning rect for window.</summary>
41     
public Rect statsRect = new Rect(0, 100, 200, 50);
42
43     ///
<summary>Unity GUI Window ID (must be unique or will cause issues).</summary>
44     
public int WindowId = 100;
45
46
47     
public void Start()
48     {
49         
this.statsRect.x = Screen.width - this.statsRect.width;
50     }

51
52     ///
<summary>Checks for shift+tab input combination (to toggle statsOn).</summary>
53     
public void Update()
54     {
55         
if (Input.GetKeyDown(KeyCode.Tab) && Input.GetKey(KeyCode.LeftShift))
56         {
57             
this.statsWindowOn = !this.statsWindowOn;
58             
this.statsOn = true; // enable stats when showing the window
59         }
60     }
61
62     
public void OnGUI()
63     {
64         
if (PhotonNetwork.networkingPeer.TrafficStatsEnabled != statsOn)
65         {
66             PhotonNetwork.networkingPeer.TrafficStatsEnabled =
this.statsOn;
67         }
68
69         
if (!this.statsWindowOn)
70         {
71             
return;
72         }
73
74         
this.statsRect = GUILayout.Window(this.WindowId, this.statsRect, this.TrafficStatsWindow, "Messages (shift+tab)");
75     }
76
77     
public void TrafficStatsWindow(int windowID)
78     {
79         
bool statsToLog = false;
80         TrafficStatsGameLevel gls = PhotonNetwork.networkingPeer.TrafficStatsGameLevel;
81         
long elapsedMs = PhotonNetwork.networkingPeer.TrafficStatsElapsedMs / 1000;
82         
if (elapsedMs == 0)
83         {
84             elapsedMs =
1;
85         }
86
87         GUILayout.BeginHorizontal();
88         
this.buttonsOn = GUILayout.Toggle(this.buttonsOn, "buttons");
89         
this.healthStatsVisible = GUILayout.Toggle(this.healthStatsVisible, "health");
90         
this.trafficStatsOn = GUILayout.Toggle(this.trafficStatsOn, "traffic");
91         GUILayout.EndHorizontal();
92         
93         
string total = string.Format("Out|In|Sum:\t{0,4} | {1,4} | {2,4}", gls.TotalOutgoingMessageCount, gls.TotalIncomingMessageCount, gls.TotalMessageCount);
94         
string elapsedTime = string.Format("{0}sec average:", elapsedMs);
95         
string average = string.Format("Out|In|Sum:\t{0,4} | {1,4} | {2,4}", gls.TotalOutgoingMessageCount / elapsedMs, gls.TotalIncomingMessageCount / elapsedMs, gls.TotalMessageCount / elapsedMs);
96         GUILayout.Label(total);
97         GUILayout.Label(elapsedTime);
98         GUILayout.Label(average);
99
100         
if (this.buttonsOn)
101         {
102             GUILayout.BeginHorizontal();
103             
this.statsOn = GUILayout.Toggle(this.statsOn, "stats on");
104             
if (GUILayout.Button("Reset"))
105             {
106                 PhotonNetwork.networkingPeer.TrafficStatsReset();
107                 PhotonNetwork.networkingPeer.TrafficStatsEnabled =
true;
108             }
109             statsToLog = GUILayout.Button(
"To Log");
110             GUILayout.EndHorizontal();
111         }
112
113         
string trafficStatsIn = string.Empty;
114         
string trafficStatsOut = string.Empty;
115         
if (this.trafficStatsOn)
116         {
117             trafficStatsIn =
"Incoming: " + PhotonNetwork.networkingPeer.TrafficStatsIncoming.ToString();
118             trafficStatsOut =
"Outgoing: " + PhotonNetwork.networkingPeer.TrafficStatsOutgoing.ToString();
119             GUILayout.Label(trafficStatsIn);
120             GUILayout.Label(trafficStatsOut);
121         }
122
123         
string healthStats = string.Empty;
124         
if (this.healthStatsVisible)
125         {
126             healthStats =
string.Format(
127                 
"ping: {6}[+/-{7}]ms\nlongest delta between\nsend: {0,4}ms disp: {1,4}ms\nlongest time for:\nev({3}):{2,3}ms op({5}):{4,3}ms",
128                 gls.LongestDeltaBetweenSending,
129                 gls.LongestDeltaBetweenDispatching,
130                 gls.LongestEventCallback,
131                 gls.LongestEventCallbackCode,
132                 gls.LongestOpResponseCallback,
133                 gls.LongestOpResponseCallbackOpCode,
134                 PhotonNetwork.networkingPeer.RoundTripTime,
135                 PhotonNetwork.networkingPeer.RoundTripTimeVariance);
136             GUILayout.Label(healthStats);
137         }
138
139         
if (statsToLog)
140         {
141             
string complete = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}", total, elapsedTime, average, trafficStatsIn, trafficStatsOut, healthStats);
142             Debug.Log(complete);
143         }
144
145         
// if anything was clicked, the height of this window is likely changed. reduce it to be layouted again next frame
146         
if (GUI.changed)
147         {
148             
this.statsRect.height = 100;
149         }
150
151         GUI.DragWindow();
152     }
153 }


\file

Part of the [Optional GUI](@ref optionalGui).

Basic GUI to show traffic and health statistics of the connection to Photon,

toggled by shift+tab.

The shown health values can help identify problems with connection losses or performance.

Example:

If the time delta between two consecutive SendOutgoingCommands calls is a second or more,

chances rise for a disconnect being caused by this (because acknowledgements to the server

need to be sent in due time).

\ingroup optionalGui

Shows or hides GUI (does not affect if stats are collected).

Option to turn collecting stats on or off (used in Update()).

Shows additional "health" values of connection.

Shows additional "lower level" traffic stats.

Show buttons to control stats and reset them.

Positioning rect for window.

Unity GUI Window ID (must be unique or will cause issues).

Checks for shift+tab input combination (to toggle statsOn).

this.statsOn = true; enable stats when showing the window

if anything was clicked, the height of this window is likely changed. reduce it to be layouted again next frame




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.604 lượt xem

Gõ tìm kiếm nhanh...